package com.atguigu.tingshu.account.api;

import com.alibaba.nacos.common.utils.StringUtils;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.KafkaConstant;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountLockResultVo;
import com.atguigu.tingshu.vo.account.AccountLockVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

@Slf4j
@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account")
@SuppressWarnings({"all"})
public class UserAccountApiController {

    @Autowired
    private UserAccountService userAccountService;
    @GuiGuLogin
    @Operation(summary = "获取用户消费记录")
    @GetMapping("/userAccount/findUserConsumePage/{page}/{limit}")
    public Result getUserConsumePage(
            @Parameter(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,

            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit) {
        // 获取到用户Id
        Long userId = AuthContextHolder.getUserId();
        Page<UserAccountDetail> pageParam = new Page<>(page, limit);
        IPage<UserAccountDetail> pageModel = userAccountService.getUserRechargePage(pageParam, userId);
        return Result.ok(pageModel);
    }

    /**
     * 用户充值记录
     * @param page
     * @param limit
     * @return
     */
    @GuiGuLogin
    @Operation(summary = "获取用户充值记录")
    @GetMapping("/userAccount/findUserRechargePage/{page}/{limit}")
    public Result findUserRechargePage(
            @Parameter(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,

            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit) {
        // 获取到用户Id
        Long userId = AuthContextHolder.getUserId();
        // 构建Page对象
        Page<UserAccountDetail> pageParam = new Page<>(page, limit);
        // 调用服务层方法
        IPage<UserAccountDetail> pageModel = userAccountService.getUserRechargePage(pageParam, userId);
        // 返回数据
        return Result.ok(pageModel);
    }
    /**
     * /api/account/rechargeInfo/getRechargeInfo/{orderNo}
     * 根据订单号获取充值信息
     */
    @Operation(summary = "根据订单号获取充值信息")
    @GetMapping("/rechargeInfo/getRechargeInfo/{orderNo}")
    public Result<RechargeInfo> getRechargeInfo(@PathVariable String orderNo){
        //调用业务层
       RechargeInfo rechargeInfo= userAccountService.getRechargeInfo(orderNo);
       return Result.ok(rechargeInfo);
    }


    /**
     * 还原锁定金额
     *
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_UNLOCK)
    public void unlock(ConsumerRecord<String, String> record) {
        String orderNo = record.value();
        if (StringUtils.isNotBlank(orderNo)) {
            log.info("【账户服务】监听到还原锁定金额消息：{}", orderNo);
            //  调用还原锁定金额
            userAccountService.unlock(orderNo);
        }
    }

    /**
     * 扣减锁定金额
     *
     * @param record
     */
    @KafkaListener(topics = KafkaConstant.QUEUE_ACCOUNT_MINUS)
    public void minus(ConsumerRecord<String, String> record) {
        String orderNo = record.value();
        if (StringUtils.isNotBlank(orderNo)) {
            log.info("【账户服务】监听到扣减锁定金额消息：{}", orderNo);
            //扣减锁定金额
            userAccountService.minus(orderNo);
        }
    }

    /**
     * /api/account/userAccount/checkAndLock
     * 检查及锁定账户金额
     */
    @GuiGuLogin
    @Operation(summary = "检查及锁定账户金额")
    @PostMapping("/userAccount/checkAndLock")
    public Result<AccountLockResultVo> checkAndLock(@RequestBody AccountLockVo accountLockVo) {
        //获取用户的id信息
        Long userId = AuthContextHolder.getUserId();
        //调用业务层
        AccountLockResultVo accountLockVo1 = userAccountService.checkAndLock(userId, accountLockVo);
        return Result.ok(accountLockVo1);
    }

    /**
     * 获取当前登录用户账户：可用余额
     * /api/account/userAccount/getAvailableAmount
     */
    @Operation(summary = "获取当前登录用户账户：可用余额")
    @GetMapping("/userAccount/getAvailableAmount")
    @GuiGuLogin
    public Result<BigDecimal> getAvailableAmount() {
        Long userId = AuthContextHolder.getUserId();
        BigDecimal bigDecimal = userAccountService.getAvailableAmount(userId);
        return Result.ok(bigDecimal);
    }
}

